package com.imagine.controller.agentA;

import com.imagine.entity.AgentA;
import com.imagine.entity.BaseResult;
import com.imagine.entity.LegalPersonalSecurityAccount;
import com.imagine.entity.PersonalSecurityAccount;
import com.imagine.service.AgentService;
import com.imagine.service.LegalPersonalSecurityAccountService;
import com.imagine.service.PersonalSecurityAccountService;
import com.imagine.utils.TimeFormat;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import sun.management.Agent;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liangyue on 2017/10/2.
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/agent/account")
public class AccountControllerDeal {


    @Autowired
    private PersonalSecurityAccountService personalSecurityAccountService;


    @Autowired
    private LegalPersonalSecurityAccountService legalPersonalSecurityAccountService;

    @Autowired
    private AgentService agentService;


    @ResponseBody
    @ApiOperation(value="A管理员登录", notes="A管理员登录")
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public BaseResult agentALogin(
            @ApiParam("用户名") @RequestParam(value = "name") String name,
            @ApiParam("密码") @RequestParam(value = "password") String password,
            HttpSession session
    ) throws IOException {
        BaseResult result = new BaseResult();
        if (agentService.getAgentAByName(name) == null){
            result.error("没有该用户信息");
            return result;
        }
        AgentA agent = agentService.getAgentAByName(name);

        if (!agent.getPassword().equals(password)){
            result.error("密码错误");
            return result;
        }
        session.setAttribute("agent", agent);
        result.setMessage("登录成功");
        return result;
    }

    @ResponseBody
    @ApiOperation(value="展示所有未审核的个人客户", notes="展示所有未审核的个人客户")
    @RequestMapping(value = "/p", method = RequestMethod.GET)
    public BaseResult getPersonalSecurityAccountUnchecked(){
        List<PersonalSecurityAccount> list = personalSecurityAccountService.getPersonalSecurityAccountUnchecked();
        BaseResult result = new BaseResult();
        result.success(list);
        return result;
    }

    @ResponseBody
    @ApiOperation(value="展示所有未审核的法人客户", notes="展示所有未审核的法人客户")
    @RequestMapping(value = "/l", method = RequestMethod.GET)
    public BaseResult getlAccountUnchecked(){
        return legalPersonalSecurityAccountService.getlAccountUnchecked();
    }



    @ResponseBody
    @ApiOperation(value="同意证券账户申请", notes="同意证券账户申请")
    @RequestMapping(value = "/agree", method = RequestMethod.POST)
    public BaseResult getPersonalSecurityAccount(
            @ApiParam("账户类型") @RequestParam(value = "type") int type,
            @ApiParam("账户id") @RequestParam(value = "idNumber") String idNumber
    ) throws IOException {
        //如果用户是个人账户
        if (type == 0){
            return personalSecurityAccountService.updatepAccountStateByPIdNumber(idNumber, 2);
        }

        else return legalPersonalSecurityAccountService.updatelAccountStateBylIdNumber(idNumber, 2);
    }
}
